//
//  ShowItem.m
//  HYHOME
//
//  Created by Mac on 16/1/27.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "ShowItem.h"
#import "NetWorkHandler.h"
#import "CellOfPush.h"
#import "ModelImage.h"
#import "ShowItem2.h"
#import "NetworkingHandle.h"
#import "DKNightVersion.h"

@interface ShowItem ()<UICollectionViewDataSource,UICollectionViewDelegate,NetWorkHandlerDelegate,UITextFieldDelegate>


@property (nonatomic,retain)UICollectionViewFlowLayout *flowLayout;
@property (nonatomic,retain)UICollectionView *collection;

@property (nonatomic,retain)NSMutableArray *arrM;
@property (nonatomic,retain)NSMutableArray *arrUrl;


@end

@implementation ShowItem
- (void)dealloc{

    [_collection release];
    self.collection.delegate = nil;
    self.collection.dataSource = nil;
    [super dealloc];



}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self handleData];
//    [self handle];
    [self createCollection];
}



- (void)handle{
    
    self.arrUrl = [NSMutableArray array];

    NSString *str =  [NSString stringWithFormat:@"http://yyjd.xinzhuzhang.com/m/jiaju/goods/detail?id=%ld&appid=881376723&channel=AppleStore2&token=&udid=852f2d86c0414900d4b72ef95c09ccf6d39d87c2&idfa=EEACF35A-5B73-4678-B6EF-E188A26B0232&idfv=E60577C5-F045-4F8D-99F2-A98F40DF00AB&mac=02:00:00:00:00:00&timestamp=1453897684.261771&v=1.2.0&ios=9.1&sign=d20a1f34b9402133d82291e5f6895327", (long)self.ida];
    
    
    [NetworkingHandle networkingWithURL:str completeHandel:^(NSURLResponse *response, NSData *data, id result) {
        
        
        
        
        NSDictionary *dic = [result objectForKey:@"goods"];
            
        ModelImage *model = [[ModelImage alloc]init];
        
        model.url = [dic objectForKey:@"url"];
        model.ida = [dic objectForKey:@"id"];
        
        [self.arrUrl addObject:model];
        
        
        [self.collection reloadData];
        [model release];
    }];
    


}


- (void)handleData{

    NSString *str = [NSString stringWithFormat:@"http://yyjd.xinzhuzhang.com/m/jiaju/goods/category/search?cid=%ld&isFollow=&page=0&appid=887723574&channel=AppleStore2&token=&udid=20b53f5724c15bc5a67a488741611028ec9860dd&idfa=EEACF35A-5B73-4678-B6EF-E188A26B0232&idfv=EC04F884-D14A-47E4-9F4E-773F9748EB95&mac=02:00:00:00:00:00&timestamp=1453882442.118381&v=1.1.6&ios=9.1&sign=ae48e22db212bcd9ceecd076673850ad",(long)self.catid];
    
    [NetWorkHandler handlerJSONWithURL:str delegate:self];


}

-(void)haneldDidComplete:(id)result{
    
    self.arrM = [NSMutableArray array];
    
    self.arrUrl = [NSMutableArray array];
    
    NSArray *arr = [result objectForKey:@"myShopGoodsList"];
    
    for (NSDictionary *dic in arr) {
        
        ModelImage *model = [[ModelImage alloc]init];
        
        model.pic = [dic objectForKey:@"pic"];
        model.price = [dic objectForKey:@"price"];
        model.ida = [dic objectForKey:@"id"];

        
        [self.arrM addObject:model];
        [model release];
        
    }
    
    [self.collection reloadData];

}

- (void)createCollection{


    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    self.flowLayout.itemSize = CGSizeMake((self.view.frame.size.width - 50) / 3.0, ((self.view.frame.size.width - 50) / 3.0) * 1.618);
    
    // 横向
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
    
    
    
    self.collection = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:self.flowLayout];
    
    [self.view addSubview:self.collection];
    
    self.collection.dataSource = self;
    
    self.collection.delegate = self;
    
    self.collection.backgroundColor = [UIColor whiteColor];

    [self.collection registerClass:[CellOfPush class] forCellWithReuseIdentifier:@"poolpush"];
    
    self.collection.showsHorizontalScrollIndicator = NO;
    self.collection.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
    
    
    [self.flowLayout release];
    [self.collection release];

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return  self.arrM.count ;


}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{


    CellOfPush *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolpush" forIndexPath:indexPath];
    
    
    ModelOfPush *model = [self.arrM objectAtIndex:indexPath.item];
    [cell passModel:model];

    
    return cell;




}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    ShowItem2 *show = [[ShowItem2 alloc]init];
    

    
    
    ModelImage *model = [self.arrM objectAtIndex:indexPath.item];
    
    show.ida = model.ida;
    
    [self.navigationController pushViewController:show animated:YES];
    
    [show release];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
